#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int a[105];
vector<int> cand;
int dp[105][1 << 10][1 << 10];
void init() {
  int mask = (1 << m) - 1;
  rep(i, 0, mask) {
    int j = i >> 1;
    int k = i >> 2;
    if (!(i & j) && !(i & k)) cand.push_back(i);
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) {
    string s;
    cin >> s;
    int len = s.size();
    rep(j, 0, len - 1) if (s[j] == 'H') a[i] |= 1 << j;
  }
  init();
  int mx = (1 << m) - 1;
  for (int u : cand) {
    if (u & a[1]) continue;
    for (int v : cand) {
      if (v & a[2]) continue;
      if (!(u & v)) dp[2][u][v] = __builtin_popcount(u) + __builtin_popcount(v);
    }
  }
  rep(i, 3, n + 2) {
    for (int u : cand) {
      if (u & a[i - 2]) continue;
      for (int v : cand) {
        if (v & a[i - 1]) continue;
        if (u & v) continue;
        for (int w : cand) {
          if (w & a[i]) continue;
          if ((u & w) || (v & w)) continue;
          dp[i][v][w] =
              max(dp[i][v][w], dp[i - 1][u][v] + __builtin_popcount(w));
        }
      }
    }
  }
  cout << dp[n + 2][0][0];
  return 0;
}